﻿using System.IO;
using System.Runtime.InteropServices;
using System.Text;

namespace RxjhServer
{
    class Ini
    {
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

        private const string section = "GameServer";

        private readonly string filePath;

        public Ini(string filePath)
        {
            this.filePath = filePath;
        }

        public string GetString(string key)
        {
            if (File.Exists(filePath))
            {
                int size = 1024;
                string def = string.Empty;
                StringBuilder retVal = new StringBuilder(size);
                GetPrivateProfileString(section, key, def, retVal, size, filePath);
                return retVal.ToString();
            }
            else
            {
                return string.Empty;
            }
        }

        public bool SetString(string key, string val)
        {
            return File.Exists(filePath) && WritePrivateProfileString(section, key, val, filePath) == 1;
        }

    }
}
